package com.zhao.controller.show;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.*;
import com.zhao.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/show")
public class ShowController {
    @Autowired
    SelfhelpService selfhelpService;
    @Autowired
    ArticleService articleService;
    @Autowired
    GroupsService groupsService;
    @Autowired
    StatisticService statisticService;
    @Autowired
    CommentService commentService;
    @Autowired
    DatafileService datafileService;
    @Autowired
    FileStatisticService fileStatisticService;
    @Autowired
    FileCommentService fileCommentService;
    @Autowired
    TransactionService transactionService;
    @Autowired
    WalletService walletService;
    @Autowired
    ChartService chartService;
    @Autowired
    AnnouncementService announcementService;
    @Autowired
    FeedbackService feedbackService;
    @Autowired
    CalendarService calendarService;
    /**
     * 主页
     * url:/show/main
     * 功能：
     *       1.文章数据
     *       2.文章查询
     *       3.热门推荐数据
     *       4.最新评论
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String index(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                        @RequestParam(value = "words",required = false)String words,
                        Model model){
        //查询热门推荐
        List<Article> list=articleService.host(5);
        //获取全部文章 并分页查询每页固定8条 模糊查询
        Page<Article> page1=articleService.selectListPage(pageNum, 8,words,null,"1");
        //获取最新评论
        QueryWrapper<Comment> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 5");
        List<Comment> comments = commentService.list(wrapper);
        //获取公告
        List<Announcement> announcements = announcementService.list(new QueryWrapper<Announcement>().eq("is_show", 0).orderByDesc("create_time"));
        if (Objects.isNull(announcements)){
            model.addAttribute("announcement",new Announcement());
        }else {
            model.addAttribute("announcement",announcements.get(0));
        }

        model.addAttribute("comments",comments);
        model.addAttribute("words",words);
        model.addAttribute("pages",page1);
        model.addAttribute("articles",list);
        return "show/main";
    }
    //资源中心页面
    @RequestMapping("/download")
    public String download(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                        @RequestParam(value = "words",required = false)String words,
                        Model model){
        //查询热门推荐
        List<Article> list=articleService.host(5);
        //获取全部资源 并分页查询每页固定8条 模糊查询
        Page<Datafile> page1=datafileService.selectListPage(pageNum, 8,words,null,"1");
        //获取最新评论
        QueryWrapper<Comment> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 5");
        List<Comment> comments = commentService.list(wrapper);

        model.addAttribute("comments",comments);
        model.addAttribute("words",words);
        model.addAttribute("pages",page1);
        model.addAttribute("articles",list);
        return "show/download";
    }
    //单个资源
    @RequestMapping("/fileContent/{id}")
    public String fileContent(@PathVariable("id")Integer id,Model model){

        //查询热门推荐
        List<Article> list=articleService.host(5);
        //获取最新评论
        QueryWrapper<Comment> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 5");
        List<Comment> comments = commentService.list(wrapper);
        //获取资源的信息
        Datafile datafile = datafileService.getById(id);
        //获取资源的统计信息
        Filestatistic one = fileStatisticService.getOne(new QueryWrapper<Filestatistic>().eq("file_id", id));
        //获取资源的评论信息
        List<FileComment> fileComments = fileCommentService.allComment(id);
        //获取当前用户
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        //获取该用户是否购买过
        String buy="no";
        if (loginUser!=null){
            Transaction one1 = transactionService.getOne(new QueryWrapper<Transaction>().eq("buyers", loginUser.getUserId()).eq("file_id", datafile.getFileId()));
            if (one1!=null){
                buy="yes";
            }
        }
        //是否买过
        model.addAttribute("buy",buy);
        //评论
        model.addAttribute("fileComments",fileComments);
        //资源的信息
        model.addAttribute("datafile",datafile);
        //资源的统计信息
        model.addAttribute("statistic",one);
        //资源的评论信息
        //热门评论
        model.addAttribute("comments",comments);
        //热门推荐数据
        model.addAttribute("articles",list);
        return "show/fileContent";
    }
    //网站关于页面
    @RequestMapping("/about")
    public String about(Model model){
        //查询热门推荐
        List<Article> list=articleService.host(5);
        //获取最新评论
        QueryWrapper<Comment> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 5");
        List<Comment> comments = commentService.list(wrapper);
        //留言版
        List<Feedback> feedBacks = feedbackService.list(new QueryWrapper<Feedback>().orderByDesc("create_time").last("limit 10"));

        //留言数据
        model.addAttribute("feedBacks",feedBacks);
        model.addAttribute("comments",comments);
        //热门推荐数据
        model.addAttribute("articles",list);
        return "show/about";
    }

    //单个文章显示
    @RequestMapping("/content/{id}")
    public String content(@PathVariable("id")Integer id,Model model){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        if (loginUser!=null){
            Chart chart1 = chartService.getOne(new QueryWrapper<Chart>().eq("aid", id).eq("author", loginUser.getUserAccount()).eq("type",0));
            Chart chart2 = chartService.getOne(new QueryWrapper<Chart>().eq("aid", id).eq("author", loginUser.getUserAccount()).eq("type",1));
            if (chart1==null){
                model.addAttribute("chart1","nozan");
            }else {
                model.addAttribute("chart1", "yeszan");
            }
            if (chart2==null){
                model.addAttribute("chart2","noshou");
            }else {
                    model.addAttribute("chart2", "yesshou");
            }
        }
        //查询热门推荐
        List<Article> list=articleService.host(5);
        //增加点击量
        //根据文章id 获取 对应统计数据进行修改
        Statistic one = statisticService.getOne(new QueryWrapper<Statistic>().eq("article_id", id));
        one.setHits(one.getHits()+1);
        statisticService.updateById(one);
        //获取要显示的文章信息
        Article article = articleService.dyOne(id);
        //获取 文章分组
        Groups groups = groupsService.getById(article.getGroupsId());
        //本文全部评论
        List<Comment> comments =commentService.queryByFatherByArtid(id,0);
        List<Comment> AComments=commentService.commentSort(comments);

        //本文评论
        model.addAttribute("AComments",AComments);
        //热门评论
        /*model.addAttribute("comments",comments);*/
        //热门推荐数据
        model.addAttribute("articles",list);
        //单个文章信息
        model.addAttribute("article",article);
        //文章类型
        model.addAttribute("groups",groups);
        return "show/content";
    }

    @ResponseBody
    @RequestMapping("/right")
    public String right() {
        Calendar now = Calendar.getInstance();
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        Selfhelp selfhelp = selfhelpService.getById(now.get(Calendar.DAY_OF_MONTH));
        return selfhelp.getContent();
    }
    @RequestMapping("/toCalendar")
    public String viewCalendar(Model model){
        List<Calendars> list = calendarService.list(new QueryWrapper<Calendars>().orderByDesc("create_time"));
        for (Calendars calendars : list) {
            calendars.setBadList(Arrays.asList(calendars.getBad().split("><")));
            calendars.setBad(calendars.getBad().replace("><","  "));
            calendars.setGoodList(Arrays.asList(calendars.getGood().split("><")));
            calendars.setGood(calendars.getGood().replace("><","  "));
        }
        model.addAttribute("calendars",list);
        return "show/plate/programmerCalendar";
    }
    @RequestMapping("/utilList")
    public String utilList(Model model){

        return "show/plate/utilList";
    }
    @RequestMapping("/intelligentArticle")
    public String intelligentArticle(Model model){

        return "show/plate/intelligentArticle";
    }
    @RequestMapping("/eatWhat")
    public String eatWhat(Model model){

        return "show/plate/eatWhat";
    }
    @RequestMapping("/htmlCodeEditor")
    public String htmlCodeEditor(Model model){

        return "show/plate/htmlCodeEditor";
    }
}
